<template>
  <view class="box">
    <view class="box_view" v-for="(item, index) in listv.data" :key="index" >
      <view v-show="!flag" class="item_left_clickView" @click="change(item)">
        <image
          v-if="!item.clickable"
          class="item_left_click"
          src="../static/images/my/icon_grzy_quanxuan.png"
          mode=""
        ></image>
        <image
          v-else
          class="item_left_clickable"
          src="../static/images/my/icon_grzy_xc_xuanzhong.png"
          mode=""
        ></image>
      </view>

      <view class="box_item" :class="backBg ? 'box_item1' : ''" @click="goPage(item)">
        <view class="title" :class="backBg ? 'title1' : ''">
			<view class="titile_t">
				{{ item.title }}
			</view>
          <view
            class="type"
            :style="{
              background: bacMapping[item.condition].background,
              color: bacMapping[item.condition].color,
            }"
          >
            {{ bacMapping[item.condition].text }}
          </view>
        </view>
        <view class="content" :class="backBg ? 'content1' : ''" v-if="item.kecheng && item.kecheng.length">
          关联课程：{{item.kecheng[0].title}}
        </view>
        <view class="time"> 活动时间：{{item.start_time}}-{{item.end_time}} </view>
      </view>
    </view>

    <view class="bottom" v-show="!flag">
      <view class="bottom_left">
        <view @click="changeClick">
          <image
            v-if="!clickableAll"
            class="item_left_click"
            src="../static/images/my/icon_grzy_quanxuan.png"
            mode=""
          ></image>
          <image
            v-else
            class="item_left_click"
            src="../static/images/my/icon_grzy_xc_xuanzhong.png"
            mode=""
          ></image>
        </view>
        <view class="bottom_left_text">全选</view>
      </view>
      <view class="bottom_right" @click="deleteFn">删除</view>
    </view>

    <u-modal
      :show="show"
      :title="title"
      :content="content"
      confirmColor="#55B877"
      :closeOnClickOverlay="true"
      @confirm="confirm"
      @close="close"
      @cancel="cancel"
      :showCancelButton="true"
    ></u-modal>

    <view class="empty_view" v-if="listv.data && listv.data.length == 0">
      <image
        class="item_left_clickable"
        src="../static/images/my/kong.png"
        mode=""
      ></image>
      <view v-if="from == 'history'">您还没有历史记录~</view>
      <view v-else>当前没有相关数据，请查看其他分类或功能。</view>
    </view>
  </view>
</template>

<script>
export default {
  props: {
    flag: {
      type: Boolean,
      default: true,
    },
    backBg: {
      type: Boolean,
      default: false,
    },
    list: {
      type: Object,
      default: () => {},
    },
    from: {
      type: String,
      default: "",
    },
  },
  watch: {
    list: {
      deep: true, // 深度监听每一个属性值, 如果是对象可以深度监听
      immediate: true, // 进入页面后立即自动调用handler函数
      handler(newValue) {
        this.listv = newValue;
        if (this.listv && this.listv.data) {
          for (let i = 0; i < this.listv.data.length; i++) {
            this.listv.data[i].clickable = false;
          }
        }
        console.log(this.listv, "newList");
      },
    },
  },
  data() {
    return {
      listv:[],
      bacMapping: [
        {
          id: 1,
          background: "rgba(85,184,119,0.3)",
          text: "免费",
          color: "#55B877",
        },
        {
          id: 2,
          background: "linear-gradient(to right, #C9F3BF 0%, #8CD7A3 100%)",
          text: "元气",
          color: "#30633A",
        },
        {
          id: 3,
          background: "linear-gradient(to right, #FBF5DD 0%, #FABC81 100%)",
          text: "启初",
          color: "#634730",
        },
        {
          id: 4,
          background: "linear-gradient(to right, #D7D9F5 0%, #CFA5E3 100%)",
          text: "青空",
          color: "#442447",
        },
        {
          id: 5,
          background: "linear-gradient(to right, #BBE1FF 0%, #7EC6FF 100%)",
          text: "无极",
          color: "#303163",
        },
        {
          id: 6,
          background: "linear-gradient(to right, #D7E3EF 0%, #6C87D0 100%)",
          text: "返朴",
          color: "#303163",
        },
      ],
      clickableAll: false,
      show: false,
      title: "确认删除",
      content: "确认删除选中内容？",
    };
  },
  methods: {
    goPage(item) {
      uni.navigateTo({
        url: `/pages/training/trainDetail?id=${item.id}`,
      });
    },
    change(item) {
      this.$forceUpdate();
      item.clickable = !item.clickable;
      // 判断是否全选
      let all = true;
      for (let i = 0; i < this.listv.data.length; i++) {
        if (this.listv.data[i].clickable == false) {
          all = false;
        }
      }
      if (all) {
        this.clickableAll = true;
      } else {
        this.clickableAll = false;
      }
    },
    changeClick() {
      this.clickableAll = !this.clickableAll;
      if (this.clickableAll) {
        for (let i = 0; i < this.listv.data.length; i++) {
          this.listv.data[i].clickable = true;
        }
      } else {
        for (let i = 0; i < this.listv.data.length; i++) {
          this.listv.data[i].clickable = false;
        }
      }
    },
    confirm() {
      this.show = false;
      let arr = [];
      let num = 0;
      for (let i = 0; i < this.listv.data.length; i++) {
        if (this.listv.data[i].clickable == true) {
          arr.push(this.listv.data[i].history_id);
          num++;
        }
      }
      console.log(arr, "数组");
      this.deletFn(arr);
    },
    close() {
      this.show = false;
    },
    cancel() {
      this.show = false;
    },
    // 删除按钮
    deleteFn() {
      let isTrue = true;
      for (let i = 0; i < this.listv.data.length; i++) {
        if (this.listv.data[i].clickable == true) {
          isTrue = false;
        }
      }
      if (isTrue == true) {
        uni.$u.toast("您还未选择内容");
      } else {
        this.show = true;
      }
    },
    // 删除接口
    deletFn(arr) {
      this.$http
        .historyDel_his_xunlianying({ ids: arr })
        .then((res) => {
          uni.$u.toast(res.message);
          this.show = false;
          setTimeout(() => {
            this.$emit("updateDataXunLian");
          }, 1000);
        })
        .catch((error) => {
          uni.$u.toast(error);
        });
    },
  },
};
</script>

<style lang="scss" scoped>
.box {
  width: 100%;
  .box_view {
    // padding: 40rpx 0 0;
    box-sizing: border-box;
    display: flex;
    align-items: center;
  }
  .box_item {
    width: 100%;
    background: #f7f8fa;
    border-radius: 16rpx 16rpx 16rpx 16rpx;
    padding: 32rpx;
    box-sizing: border-box;
    margin-bottom: 32rpx;
	
    .title {
      font-size: 30rpx;
      font-family: PingFang SC-Medium, PingFang SC;
      font-weight: 400;
      color: #444444;
      display: flex;
	  
	  .titile_t {
		  width: 80%;
		  overflow: hidden;
		  text-overflow: ellipsis;
		  white-space: nowrap;
	  }
      .type {
        font-size: 24rpx;
        font-family: PingFang SC-Medium, PingFang SC;
        font-weight: 400;
        border-radius: 4rpx;
        padding: 4rpx 10rpx;
        margin-left: 24rpx;
      }
    }
    .title1 {
      color: #eee;
    }
    .content {
      font-size: 26rpx;
      font-family: PingFang SC-Medium, PingFang SC;
      font-weight: 400;
      color: #888888;
      margin-top: 16rpx;
    }
    .content1 {
      color: #eee;
    }
    .time {
      font-size: 24rpx;
      font-family: PingFang SC-Medium, PingFang SC;
      font-weight: 400;
      color: #888888;
      margin-top: 32rpx;
    }
  }
  .box_item1 {
    background: rgba(39, 39, 39, 0.5);
  }
  .item_left_clickView {
    margin-right: 24rpx;
    .item_left_click {
      width: 40rpx;
      height: 40rpx;
    }
    .item_left_clickable {
      width: 40rpx;
      height: 40rpx;
    }
  }

  .bottom {
    width: 100%;
    height: 112rpx;
    background: #272727;
    position: fixed;
    bottom: 0;
    left: 0;
    padding: 16rpx 30rpx;
    box-sizing: border-box;
    display: flex;
    align-items: center;
    justify-content: space-between;
    .bottom_left {
      display: flex;
      align-items: center;
      .item_left_click {
        width: 40rpx;
        height: 40rpx;
      }
      .bottom_left_text {
        margin-left: 16rpx;
        color: #eeeeee;
        font-size: 30rpx;
      }
    }

    .bottom_right {
      width: 250rpx;
      height: 80rpx;
      background: #ee6060;
      border-radius: 48rpx;
      text-align: center;
      line-height: 80rpx;
      font-size: 30rpx;
      color: #eeeeee;
    }
  }

  /deep/.u-modal__content__text {
    text-align: center;
  }

  .empty_view {
    margin-top: 300rpx;
    image {
      margin: 0 auto;
      width: 410rpx;
      height: 410rpx;
    }
    view {
      font-size: 30rpx;
      color: #d8d8d8;
      text-align: center;
    }
  }
}
</style>